package consys.social.core;

import consys.social.memcached.dao.spymemcached.Memcached;
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.HBaseAdmin;

/**
 * install and uninstall tables and memcached settings
 *
 * @author Petr Kunc
 */
public class Installator {

    private Memcached memcached;
    private String entities;
    private String walls;
    private String discussions;

    public void setDiscussions(String discussions) {
        this.discussions = discussions;
    }

    public void setEntities(String entities) {
        this.entities = entities;
    }

    public void setMemcached(Memcached memcached) {
        this.memcached = memcached;
    }

    public void setWalls(String walls) {
        this.walls = walls;
    }

    public void install() throws MasterNotRunningException {
        memcached.clear();
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.master", "localhost:60000");
        HBaseAdmin hbase = null;
        try {
            hbase = new HBaseAdmin(conf);
        } catch (ZooKeeperConnectionException ex) {
            System.out.println("nelze pripojit");
        }

        try {
            hbase.disableTable(entities);
            hbase.deleteTable(entities);
        } catch (IOException e) {
        }
        HTableDescriptor entTable = new HTableDescriptor(entities);
        HColumnDescriptor followers = new HColumnDescriptor("followers".getBytes());
        HColumnDescriptor following = new HColumnDescriptor("following".getBytes());
        HColumnDescriptor blocked = new HColumnDescriptor("blocked".getBytes());
	HColumnDescriptor news = new HColumnDescriptor("news".getBytes());
        HColumnDescriptor count = new HColumnDescriptor("count".getBytes());
        //HColumnDescriptor wall = new HColumnDescriptor("wall".getBytes());
        followers.setMaxVersions(1);
        following.setMaxVersions(1);
        count.setMaxVersions(1);
        entTable.addFamily(followers);
        entTable.addFamily(following);
        entTable.addFamily(count);
        entTable.addFamily(blocked);
        entTable.addFamily(news);
        //entTable.addFamily(wall);
        try {
            hbase.createTable(entTable);
        } catch (IOException ex) {
            System.out.println("nelze vytvorit");
        }
        try {
            hbase.disableTable(walls);
            hbase.deleteTable(walls);
        } catch (IOException e) {
        }
        HTableDescriptor wallTable = new HTableDescriptor(walls);
        HColumnDescriptor info = new HColumnDescriptor("info".getBytes());
        HColumnDescriptor text = new HColumnDescriptor("text".getBytes());
        HColumnDescriptor likes = new HColumnDescriptor("likes".getBytes());
        wallTable.addFamily(info);
        wallTable.addFamily(text);
        wallTable.addFamily(likes);
        try {
            hbase.createTable(wallTable);
        } catch (IOException ex) {
            System.out.println("nelze vytvorit wall");
        }
        
        try {
            hbase.disableTable(discussions);
            hbase.deleteTable(discussions);
        } catch (IOException e) {
        }
        HTableDescriptor discTable = new HTableDescriptor(discussions);
        discTable.addFamily(info);
        discTable.addFamily(text);
        try {
            hbase.createTable(discTable);
        } catch (IOException ex) {
            System.out.println("nelze vytvorit disk");
        }
    }

    public void uninstall() throws MasterNotRunningException, ZooKeeperConnectionException {
        memcached.clear();
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.master", "localhost:60000");
        HBaseAdmin hbase = new HBaseAdmin(conf);
        try {
            hbase.disableTable(entities);
            hbase.deleteTable(entities);
        } catch (IOException ex) {
        }
        try {
            hbase.disableTable(walls);
            hbase.deleteTable(walls);
        } catch (IOException ex) {
        }
        try {
            hbase.disableTable(discussions);
            hbase.deleteTable(discussions);
        } catch (IOException ex) {
        }
    }
}
